package com.jh3502.vivo.function.citylist;

/**
 * @author harisucici
 * @date 2017/6/19
 */

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import com.jh3502.vivo.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class CityListAdapter extends RecyclerView.Adapter<CityListAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private List<CityBean> dataList;
    private List<CityBean> dataChosen;
    private View convertView;
    @Nullable
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;


    public CityListAdapter(Context context, List<CityBean> dataList, List<CityBean> dataChosen) {
        this.mContext = context;
        this.dataList = dataList;
        this.dataChosen = dataChosen;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_city_list, viewGroup, false);
        ViewHolder vh = new ViewHolder(convertView);
        convertView.setOnClickListener(this);
        return vh;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.itemCityCheck.setChecked(false);// 必须初始化
        viewHolder.itemCityName.setText(dataList.get(position).getName());
        String currentStr = dataList.get(position).getNameSort();
        String previewStr = (position - 1) >= 0 ? dataList.get(position - 1).getNameSort() : " ";
        if (!previewStr.equals(currentStr)) {
            viewHolder.itemCityTitle.setVisibility(View.VISIBLE);
            viewHolder.itemCityTitle.setText(currentStr);
        } else {
            viewHolder.itemCityTitle.setVisibility(View.GONE);
        }
        for (CityBean cityBean : dataChosen) {
            if (cityBean.getAdcode().equals(dataList.get(position).getAdcode())) {
                viewHolder.itemCityCheck.setChecked(true);
            }
        }

    }


    @Override
    public void onClick(@NonNull View v) {
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick((int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    @Override
    public int getItemCount() {
        return dataList.size();
    }


    public interface OnRecyclerViewItemClickListener {
        void onItemClick(int s);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_city_title)
        TextView itemCityTitle;
        @BindView(R.id.item_city_name)
        TextView itemCityName;
        @BindView(R.id.item_city_check)
        CheckBox itemCityCheck;

        public ViewHolder(@NonNull View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}